package TankGame;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;

// 坦克类
class Tank {
    int x, y; // 坦克坐标
    int speed = 10; // 移动速度
    boolean up, down, left, right; // 方向标志位
    double angle = 0; // 射击角度
    private int health = 100; // 玩家坦克的血量

    public Tank(int startX, int startY) {
        this.x = startX;
        this.y = startY;
    }

    public void move(List<Obstacle> obstacles, List<EnemyTank> enemyTanks, int gameWidth, int gameHeight) {
        int newX = x;
        int newY = y;

        if (up && y > 0) newY -= speed;
        if (down && y < gameHeight - 50) newY += speed;
        if (left && x > 0) newX -= speed;
        if (right && x < gameWidth - 50) newX += speed;

        // 检查新位置是否与任何障碍物相交
        boolean canMove = true;
        for (Obstacle obstacle : obstacles) {
            if (new Rectangle(newX, newY, 50, 50).intersects(obstacle.getBounds())) {
                canMove = false;
                break;
            }
        }

        // 检查新位置是否与任何敌方坦克相交
        for (EnemyTank enemyTank : enemyTanks) {
            if (new Rectangle(newX, newY, 50, 50).intersects(enemyTank.getBounds())) {
                canMove = false;
                break;
            }
        }

        // 检查新位置是否超出游戏边界
        if (newX < 0 || newX > gameWidth - 60 || newY < 0 || newY > gameHeight - 50) {
            canMove = false;
        }

        if (canMove) {
            x = newX;
            y = newY;
        }
    }

    public void draw(Graphics2D g) {
        g.setColor(Color.BLUE);
        g.fillOval(x, y, 50, 50); // 简单绘制一个圆形作为坦克

        // 绘制血量条
        g.setColor(Color.GREEN);
        g.fillRect(x, y - 10, (int) (50 * ((double) health / 100)), 5);
        g.setColor(Color.BLACK);
        g.drawRect(x, y - 10, 50, 5);
    }

    // 新增方法：减少血量
    public void takeDamage(int damage) {
        health -= damage;
    }

    // 新增方法：检查坦克是否被摧毁
    public boolean isDestroyed() {
        return health <= 0;
    }

    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                up = true;
                angle = Math.PI / 2;
                break;
            case KeyEvent.VK_DOWN:
                down = true;
                angle = 3 * Math.PI / 2;
                break;
            case KeyEvent.VK_LEFT:
                left = true;
                angle = Math.PI;
                break;
            case KeyEvent.VK_RIGHT:
                right = true;
                angle = 0;
                break;
        }
    }

    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                up = false;
                break;
            case KeyEvent.VK_DOWN:
                down = false;
                break;
            case KeyEvent.VK_LEFT:
                left = false;
                break;
            case KeyEvent.VK_RIGHT:
                right = false;
                break;
        }
    }

    public Bullet shoot() {
        return new Bullet(x + 25, y + 25, angle, true); // 玩家子弹
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, 50, 50);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}